home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / ASM16 / EXCEP32.ASM < prev    next >
Assembly Source File  |  1994-12-12  |  9KB  |  427 lines

  1. ;
  2. ; EXCEP32.ASM (c) Rainer Schnitker 92,93
  3. ;
  4.  
  5. ; contains:
  6. ;
  7. ; Exception handler entry
  8. ; Ints that came from real-mode
  9. ; Other utils
  10. ;
  11.  
  12.     INCLUDE REGS386.INC
  13.  
  14.     .386P
  15.  
  16.  
  17. DGROUP    group _DATA
  18.  
  19. IFDEF    __WASM__
  20.     MYEXCEP13      equ myexcep13_
  21.     SWAPPER       equ swapper_
  22.     extrn myexcep13_:near        ; C-func:exception hander
  23.     extrn swapper_:near
  24. ELSE
  25.     MYEXCEP13      equ _myexcep13
  26.     BACK_FROM_SYSCALL equ _back_from_syscall
  27.     SWAPPER       equ _swapper
  28.     extrn _myexcep13:near        ; C-func:exception hander
  29.     extrn _swapper:near
  30. ENDIF
  31.     extrn _back_from_syscall:near
  32.  
  33. _DATA    segment word public 'DATA' use16
  34.  
  35.     extrn _code16sel:word        ;  16 bit selectors
  36.     extrn _data16sel:word        ;
  37.     extrn _stack16sel:word        ;
  38.     extrn _stackp16:dword        ;  16 bit stackpointer
  39.  
  40.     extrn _npz:PROCESS        ;  current process
  41.     extrn _regf:REG386        ;  regs for exceptions
  42.     extrn _reg_info:REGEXCEPT10    ;  ext. exception info
  43.  
  44.     public _time_tic
  45.     public _cbrkcall
  46.     _time_tic        dd    0
  47.     _cbrkcall        dw    0
  48.  
  49.     dpmiss            dw    ?
  50.     dpmiesp         dd    ?
  51.  
  52. _DATA    ends
  53.  
  54. _TEXT segment dword public 'CODE' use16
  55.     assume    cs:_TEXT,ds:DGROUP
  56.  
  57.  
  58.     public _extender_ds
  59.     _extender_ds        dw    ?
  60.     fill_ds         dw    ?
  61.  
  62.  
  63.     public _load_ds
  64. _load_ds proc near        ; load our extender ds
  65.     push    cs:[_extender_ds]
  66.     pop    ds
  67.     ret
  68. _load_ds endp
  69.  
  70.     public _clearregs
  71. _clearregs proc near                ; clear high bits
  72.     and    eax, dword ptr 0ffffH        ; for 16 bit code
  73.     and    ebx, dword ptr 0ffffH
  74.     and    ecx, dword ptr 0ffffH
  75.     and    edx, dword ptr 0ffffH
  76.     and    esi, dword ptr 0ffffH
  77.     and    edi, dword ptr 0ffffH
  78.     and    ebp, dword ptr 0ffffH
  79.     ret
  80. _clearregs endp
  81.  
  82. ; this proc is called after exception handler return from DPMI-server
  83. ; cs:eip and ss:esp set
  84. exception_after_return proc near    ; called after FAR-RET from exception
  85.     mov    ax, ss            ; get extender ds
  86.     mov    ds, ax
  87.     mov    es, ax            ; ds = es = ss
  88.  
  89.     call    _clearregs
  90.     call    MYEXCEP13
  91.  
  92.     jmp    _back_from_syscall
  93. exception_after_return endp
  94.  
  95. ; EXCEPTIONS
  96. ;
  97. ;  DPMI-rules:
  98. ; - return with far return , org SS:ESP,CS:EIP,EFLAGS(i-flag!) will restored
  99. ;   (note: iret doesn't restore i-flag, because IOPL < DPL )
  100. ; - all fault have error code (only valid for 08,0A-0E)
  101. ; - handler must preserve and restore all registers
  102. ; - handler will be called on a locked stack with interrupts disabled
  103. ; - handler must return or jump to the next handler
  104. ; - handler can modify exception stack, but not return cs,eip
  105. ;   it must return to the orginal handler
  106. ; - called only for protected mode exceptions
  107. ;
  108. ;     exception STACK
  109. ;       0 : ebp  (our,not default)
  110. ;       4 : return eip (to host)
  111. ;       8 : return cs  (to host)
  112. ;      12 : error
  113. ;      16 : EIP (orginal)
  114. ;      20 : CS (orginal)
  115. ;      24 : Eflags
  116. ;      28 : ESP
  117. ;      32 : SS
  118.  
  119. align 4
  120.     public _excep13_386
  121. _excep13_386 proc far
  122.     push    word ptr 13
  123.     jmp    short exceptionhandler
  124.  
  125. public _excep14_386
  126. _excep14_386:
  127.     push    word ptr 14
  128.     jmp    short exceptionhandler
  129.  
  130. public _excep0_386
  131. _excep0_386:
  132.     push    word ptr 0
  133.     jmp    short exceptionhandler
  134.  
  135. public _excep1_386
  136. _excep1_386:
  137.     push    word ptr 1
  138.     jmp    short exceptionhandler
  139.  
  140. public _excep2_386
  141. _excep2_386:
  142.     push    word ptr 2
  143.     jmp    short exceptionhandler
  144.  
  145. public _excep3_386
  146. _excep3_386:
  147.     push    word ptr 3
  148.     jmp    short exceptionhandler
  149.  
  150. public _excep4_386
  151. _excep4_386:
  152.     push    word ptr 4
  153.     jmp    short exceptionhandler
  154.  
  155. public _excep5_386
  156. _excep5_386:
  157.     push    word ptr 5
  158.     jmp    short exceptionhandler
  159.  
  160. public _excep6_386
  161. _excep6_386:
  162.     push    word ptr 6
  163.     jmp    short exceptionhandler
  164.  
  165. public _excep7_386
  166. _excep7_386:
  167.     push    word ptr 7
  168.     jmp    short exceptionhandler
  169.  
  170. public _excep8_386
  171. _excep8_386:
  172.     push    word ptr 8
  173.     jmp    short exceptionhandler
  174.  
  175. public _excep9_386
  176. _excep9_386:
  177.     push    word ptr 9
  178.     jmp    short exceptionhandler
  179.  
  180. public _excep10_386
  181. _excep10_386:
  182.     push    word ptr 10
  183.     jmp    short exceptionhandler
  184.  
  185. public _excep11_386
  186. _excep11_386:
  187.     push    word ptr 11
  188.     jmp    short exceptionhandler
  189.  
  190. public _excep12_386
  191. _excep12_386:
  192.     push    word ptr 12
  193.     jmp    short exceptionhandler
  194.  
  195. public _excep15_386
  196. _excep15_386:
  197.     push    word ptr 15
  198.     jmp    short exceptionhandler
  199.  
  200. public _excep16_386
  201. _excep16_386:
  202.     push    word ptr 16
  203.     jmp    short exceptionhandler
  204.  
  205. public _excep17_386
  206. _excep17_386:
  207.     push    word ptr 17
  208.     jmp    short exceptionhandler
  209.  
  210. align 4
  211. exceptionhandler:
  212.     push    ds                ; save user ds
  213.     call    _load_ds            ; we need our extender ds
  214.  
  215.     mov    dword ptr DGROUP:_regf.REG_EAX, eax
  216.     mov    dword ptr DGROUP:_regf.REG_EBX, ebx
  217.     mov    dword ptr DGROUP:_regf.REG_ECX, ecx
  218.     mov    dword ptr DGROUP:_regf.REG_EDX, edx
  219.     mov    dword ptr DGROUP:_regf.REG_EBP, ebp
  220.     mov    dword ptr DGROUP:_regf.REG_EDI, edi
  221.     mov    dword ptr DGROUP:_regf.REG_ESI, esi
  222.     mov    word ptr DGROUP:_regf.REG_ES , es
  223.     mov    word ptr DGROUP:_regf.REG_FS , fs
  224.     mov    word ptr DGROUP:_regf.REG_GS , gs
  225.  
  226.     pop    ax
  227.     mov    word ptr DGROUP:_regf.REG_DS, ax
  228.     pop    ax
  229.     mov    word ptr DGROUP:_regf.REG_FAULTNO, ax
  230.  
  231.     ; the rest regs are on stack
  232.     push    ebp
  233.     mov    ebp, esp
  234.  
  235.     ; get error code from stack
  236.     mov    eax,dword ptr [ebp+12]
  237.     mov    dword ptr DGROUP:_regf.REG_ERR, eax
  238.  
  239.     ; get eip from stack
  240.     mov    eax,dword ptr [ebp+16]
  241.     mov    dword ptr DGROUP:_regf.REG_EIP, eax
  242.  
  243.     ; get cs from stack
  244.     mov    eax,dword ptr [ebp+20]
  245.     mov    dword ptr DGROUP:_regf.REG_CS, eax
  246.  
  247.     ; get eflags from stack
  248.     mov    eax, dword ptr [ebp+24]
  249.     and    ax,0FEFFh                ; clear trace flag
  250.     or    ax, 0200h                ; set iret flag
  251.     mov    dword ptr DGROUP:_regf.REG_EFLAGS, eax    ; else trap after fret
  252.     mov    dword ptr [ebp+24],eax            ; back on dpmi-stack
  253.  
  254.     ; get esp from stack
  255.     mov    eax, dword ptr [ebp+28]
  256.     mov    dword ptr DGROUP:_regf.REG_ESP, eax
  257.     mov    dword ptr DGROUP:_regf.REG_ESPORG, eax
  258.  
  259.     ; get ss from stack
  260.     mov    ax,word ptr [ebp+32]
  261.     mov    word ptr DGROUP:_regf.REG_SS, ax
  262.  
  263.     ; set new return address cs:eip
  264.     ; to exception_after_return
  265.  
  266.     xor    eax,eax
  267.     mov    ax,offset exception_after_return
  268.     mov    dword ptr [ebp+16], eax         ; set new eip
  269.  
  270.     mov    ax,cs                    ; bzw _code16sel
  271.     mov    word ptr [ebp+20],ax            ; set cs
  272.  
  273.  
  274.     ; set new ss:esp
  275.     ; to our C gp_fault handler
  276.  
  277.     mov    si, word ptr DGROUP:_npz    ; load process ptr
  278.     mov    eax, dword ptr [si].kstack
  279.     mov    dword ptr [ebp+28],eax        ; set esp
  280.  
  281.     mov    ax,ds                ; bzw _stack16sel
  282.     mov    word ptr [ebp+32],ax        ; set ss
  283.  
  284.     pop ebp
  285.  
  286.     ; restore changed regs
  287.     mov    esi, dword ptr DGROUP:_regf.REG_ESI
  288.     mov    eax, dword ptr DGROUP:_regf.REG_EAX
  289.     push    word ptr _regf.REG_DS
  290.     pop    ds
  291.  
  292.     db 066H                 ; we need 16:32 bit return
  293.     retf
  294. _excep13_386 endp
  295.  
  296. ;
  297. ; EXCEPTIONS DPMI 1.0
  298. ;
  299. ; extended info at esp + 32
  300. ;      32 : ebp  (our,not default)
  301. ;      36 : return eip (to host)
  302. ;      40 : return cs  (to host)
  303. ;      44 : error code
  304. ;      48 : EIP (orginal)
  305. ;      52 : CS (orginal)
  306. ;      56 : EFLAGS
  307. ;      60 : ESP
  308. ;      64 : SS
  309. ;      68 : DS
  310. ;      72 : ES
  311. ;      76 : FS
  312. ;      80 : GS
  313. ;      84 : CR2
  314. ;      88 : PTE
  315.  
  316. ;
  317. ; only page fault (exception 14) use this
  318. ;
  319. public _page_fault
  320. _page_fault    proc  far
  321.     push    ebp
  322.     mov    ebp, esp
  323.     push    ds
  324.     push    es
  325.     pushad
  326.  
  327.     call    _load_ds                ; get:
  328.     mov    eax, dword ptr [ebp+44]         ; error code
  329.     mov    dword ptr DGROUP:_reg_info.RE_ERR, eax
  330.     mov    eax, dword ptr [ebp+48]         ; EIP
  331.     mov    dword ptr DGROUP:_reg_info.RE_EIP, eax
  332.     movzx    eax, word ptr [ebp+52]            ; CS
  333.     mov    dword ptr DGROUP:_reg_info.RE_CS, eax
  334.     mov    eax, dword ptr [ebp+84]         ; CR2
  335.     mov    dword ptr DGROUP:_reg_info.RE_CR2, eax    ;
  336.     mov    eax, dword ptr [ebp+88]         ; PTE
  337.     mov    dword ptr DGROUP:_reg_info.RE_PTE, eax
  338.  
  339.     mov    dpmiss, ss
  340.     mov    dpmiesp, esp
  341.     mov    ax, ds
  342.     mov    es, ax
  343.     mov    ss, ax
  344.     mov    esp, _stackp16
  345.     mov    ax, 0901h                ; enable ints
  346.     int    031h
  347.  
  348.     call    SWAPPER                 ; call swapper
  349.     or    ax, ax                    ; uncommit fault?
  350.     jz    short @@swapper_return            ; yes, return
  351.  
  352.     mov    ss, dpmiss
  353.     mov    esp, dpmiesp
  354.     popad
  355.     nop
  356.     pop    es
  357.     pop    ds
  358.     pop    ebp
  359.     push    word ptr 14                ; no, generic fault
  360.     jmp    exceptionhandler
  361.  
  362. @@swapper_return:
  363.     mov    ss, dpmiss
  364.     mov    esp, dpmiesp
  365.     popad
  366.     nop
  367.     pop    es
  368.     pop    ds
  369.     pop    ebp
  370.  
  371.     db 066H                 ; we need 16:32 bit return
  372.     retf
  373. _page_fault endp
  374.  
  375.  
  376. ;
  377. ; INTs that came form real mode (timer 0x1C,^C 0x23,crit error 0x24)
  378. ;
  379. ;  DPMI-rules:
  380. ; - handler must return ( don't terminate )
  381. ; - interrupts are disabled (?)
  382. ; - stack: locked protected mode stack from host (4 KB)
  383. ;
  384.  
  385. ; Control-C handler
  386.     public _prot_cbrk
  387. _prot_cbrk   proc    far
  388.     push    ds
  389.     push    si
  390.     call    _load_ds
  391.     mov    word ptr DGROUP:_cbrkcall,1
  392.     mov    si,word ptr DGROUP:_npz     ; load process ptr
  393.     bts    [si].sig_raised,1
  394.     pop    si
  395.     pop    ds
  396.  
  397.     ; iret doesn't restore I-flag
  398.     push    ax
  399.     mov    ax,0901h
  400.     int    031h
  401.     pop    ax
  402.  
  403.     iretd
  404.     ; retfar caused error by qemm/qdpmi
  405. _prot_cbrk   endp
  406.  
  407.  
  408. ; Timer handler
  409.     public _timer_handler
  410. _timer_handler proc far
  411.     push    ds
  412.     call    _load_ds
  413.     add    dword ptr _time_tic ,1
  414.     pop    ds
  415.  
  416.     ; iret doesn't restore I-flag
  417.     push    ax
  418.     mov    ax,0901h
  419.     int    031h
  420.     pop    ax
  421.  
  422.     iretd
  423. _timer_handler endp
  424.  
  425. _TEXT    ends
  426.     end
  427.